﻿using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using DC2010;
using DC2010.Objects.Common;
using DC2010.Objects.Data;
using DC2010.Objects.Structures;
using DC2010_EditorData;
using SdlDotNet.Input;
using DC2010_EditorData.Dialogs;

namespace DC2012
{
    public partial class CoreEditor
    {

        void Events_MouseButtonDown(object sender, MouseButtonEventArgs e)
        {
            List<DCAnimatedSprite> dsl = Neocortex.I.IntersectsWith(Mouse.MousePosition.X, Mouse.MousePosition.Y, gameinterface);

            #region Select Tile to paint with
            if (dsl.Count > 0)
            {
                if (dsl[0].Id.Contains("TILE_"))
                {
                    selectedTile = Neocortex.I.CreateNewSprite(dsl[0]);
                    selectedTile.Animate = false;
                    selectedTile.Code = dsl[0].Id;
                }
            }
            #endregion


            //mapa
            if ((Mouse.MousePosition.X > mapOriginX) && (Mouse.MousePosition.Y > mapOriginY))
            {
                Point p = GetMapCoordinates();

                KeyValue kv = new KeyValue();
                //kv.Key = p.X + "_" + p.Y;

                //42,35 -- client area for map
                if ((p.Y <= 35) && (p.X <= 42))
                {
                    FloorInfo fi = Root.I.FloorSystem.GetInfo(ActiveMap);

                    if ((e != null) && (e.Button == MouseButton.PrimaryButton))
                    {
                        //najdi vse co na teto pozici jiz existuje
                        fi.TileLayer.ClearXY(p.X, p.Y);

                        //remove from map - gfx
                        var x = (from i in map
                                 where (i is DCAnimatedSprite) && (((DCAnimatedSprite)i).Id.Contains("_" + p.X + "_" + p.Y))
                                 select i).ToList();

                        if (x.Count > 0)
                            foreach (var o in x)
                                map.Remove(o);

                        //remove tile
                        var xx = (from i in Root.I.TileSystem.DictInfo.Values
                                  where i.Loc.X == p.X && i.Loc.Y == p.Y && i.Loc.FloorId == ActiveMap
                                  select i).FirstOrDefault();


                        if (xx != null)
                            Root.I.TileSystem.RemoveInfo(xx);

                        //adding
                        if (selectedTile != null)
                        {
                            switch (selectedTile.TileType)
                            {
                                case TileType.Teleport:
                                    {
                                        TileTeleportInfo ti = new TileTeleportInfo();
                                        ti.TileType = selectedTile.TileType;
                                        ti.Direction = selectedTile.Direction;
                                        ti.Id = ActiveMap + "_" + p.X + "_" + p.Y;
                                        ti.Loc.X = p.X;
                                        ti.Loc.Y = p.Y;
                                        ti.Loc.FloorId = ActiveMap;
                                        Root.I.TileSystem.AddInfo(ti);
                                        kv.Value = ti.Id;
                                        kv.Key = p.X + "_" + p.Y;

                                        TeleportForm tf = new TeleportForm(ti);
                                        tf.ShowDialog();
                                        break;
                                    }
                                default:
                                    {
                                        TileInfo ti = new TileInfo();
                                        ti.TileType = selectedTile.TileType;
                                        ti.Direction = selectedTile.Direction;
                                        ti.Id = ActiveMap + "_" + p.X + "_" + p.Y;
                                        ti.Loc.X = p.X;
                                        ti.Loc.Y = p.Y;
                                        ti.Loc.FloorId = ActiveMap;
                                        Root.I.TileSystem.AddInfo(ti);
                                        kv.Value = ti.Id;
                                        kv.Key = p.X + "_" + p.Y;

                                        break;
                                    }
                            }

                            fi.TileLayer.tableOfObjects.Add(kv);

                        }
                    }

                    //editing
                    if ((e != null) && (e.Button == MouseButton.SecondaryButton))
                    {
                        //tile under cursor
                        TileInfo ti = Root.I.TileSystem.GetInfo(ActiveMap + "_" + p.X + "_" + p.Y);

                        if (ti != null)
                        {
                            switch (ti.TileType)
                            {
                                case TileType.Teleport:
                                    {
                                        TeleportForm tf = new TeleportForm((TileTeleportInfo)ti);
                                        tf.ShowDialog();
                                        Root.I.TileSystem.SaveInfo();       //save changes
                                        break;
                                    }
                                default:
                                    {
                                        AddItemForm aif = new AddItemForm(ti);
                                        aif.ShowDialog();
                                        Root.I.TileSystem.SaveInfo();       //save changes
                                        break;
                                    }
                            }
                        }
                        else
                        {
                            MessageBox.Show("There is nothing to edit", "Message");
                        }
                    }


                    Root.I.FloorSystem.SaveInfo();
                }
            }
        }


    }
}
